SpringCloud Config配置中心
简介
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,
它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是Config-Server,二是Config-Client。
Config Server从本地读取配置文件
创建一个spring-boot项目,取名为 config-server,依赖如下:1
2
3
4<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
注解 @EnableConfigServer 开启配置服务器1
2
3
4
5
6
7
8
9@SpringBootApplication
@EnableConfigServer //开启配置服务器
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
需要在程序的配置文件application.properties文件配置以下。通过 spring.profile.active=native 来配置 ConfigServer 从本地读取配置,读取的路径为 classpath 下的 shared 目录。
1 | server: |
在 resources 目录下新建 shared 文件夹,在 shared 文件夹下新建 config-client-dev.yml 文件。1
2
3
4server:
port: 8762
foo: foo version 1
启动 config-server 工程!
构建Config-Client
创建一个spring-boot项目,取名为 config-client,依赖如下:1
2
3
4<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
在 resources 目录下新建 bootstrap.yml 文件,因为 bootstrap 相对于 application 具有优先的执行顺序。
变量{spring.application.name}和{spring.profiles.active},两者以“-”相连,构成了向 Config Server 读取的配置文件名。
1 | spring: |
编写一个接口,测试读取配置文件的 foo 变量,并通过 API 接口返回.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16@SpringBootApplication
@RestController
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${foo}")
String foo;
@RequestMapping(value = "/foo")
public String hi(){
return foo;
}
}
启动 config-client 工程,访问 http://localhost:8762/foo,显示
foo version 1
可见 config-client 成功向 config-server 工程读取了配置文件中 foo 变量的值。
Config Server从远程Git仓库读取配置文件
修改 config-server 的配置文件 application.yml,代码如下.1
2
3
4
5
6
7
8
9
10
11
12
13
14server:
port: 8769
spring:
application:
name: config-server
cloud:
config:
label: master
server:
git:
uri: https://github.com/forezp/SpringcloudConfig
search-paths: respo
username: miles02@163.com
password:
如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写,本例子是公开仓库,放心使用。
配置 | 解释 |
---|---|
spring.cloud.config.server.git.uri | 配置git仓库地址 |
spring.cloud.config.server.git.searchPaths | 配置仓库路径 |
spring.cloud.config.label | 配置仓库的分支 |
spring.cloud.config.server.git.username | 访问git仓库的用户名 |
spring.cloud.config.server.git.password | 访问git仓库的用户密码 |
远程仓库 https://github.com/xxxx/SpringcloudConfig/ 中有个文件config-client-dev.properties文件中有一个属性:
foo = foo version 2
但是没有规定 server.port 属性,所以会以默认 的 8080 启动,启动程序:访问http://localhost:8080/foo
foo version 2
可见,config-server 从远程 Git 仓库读取了配置文件,config-client 从config-server 读取了配置文件.
构建高可用的 Config Server
将配置中心 config-server 做成一个微服务,并且将其集群化,从而达到高可用。
config-client 在工程启动类上加上注解 @EnableEurekaClient,开启 EurekaClient的功能。
在配置文件 application.yml 加入相关配置,向 service-id 为 config-server 的配置服务读取配置文件.
1 | spring: |
启动 config-server、config-client 工程,访问 http://localhost:8762/foo,浏览器显:
foo version 2
只需要启动多个 config-server 实例即可搭建高可用的 config-server。